/// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Windows.Forms;
using NUnit.Framework;
using NUnit.Extensions.Forms;
using Microsoft.VSPowerToys.PackInstaller;
using Microsoft.VSPowerToys.Common.TaskManager.ContentInstaller;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Diagnostics;

namespace GUITestScenarios
{
    [TestFixture]
    public class ToolInstallationTests : NUnitFormTest
    {
        #region Private Data and Methods
        private PackInstallerForm packInstallForm;
        private int tripCount;
        private ErrorMessages errorMessages;

        /// <summary>
        /// In order to test forms with ShowDialog under NUnitForms, you have to
        /// provide a "handler" method for the Form. Normally, this handler would
        /// contain the test logic. But, that doesn't work well for the PackInstaller
        /// because it uses threads. 
        /// 
        /// So, the test logic will be in a delegate method attched to the PackInstaller 
        /// form. Therefore, this is a skeleton handler and used for most tests.
        /// </summary>
        private void GenericFormHandler()
        {
        }

        #endregion

        [TestFixtureSetUp]
        public void Init()
        {
            Process[] visualStudioProcesses = Process.GetProcessesByName("devenv");
            TextWriter errorWriter = Console.Error;
            if (visualStudioProcesses.Length != 0)
            {
                errorWriter.WriteLine("You must close Visual Studio before running the test suite.");
                Assert.IsTrue(false);
            }
        }

        #region Install one tool test
        /// <summary>
        /// This test installs one tool. First, it forces a download
        /// of the tool's MSI and installs the tool. Second, it removes
        /// the tool. Third, it reinstalls the tool expecting it not to download.
        /// Fourth, it will remove the tool.
        /// The test covers the following scenarios from test plan: 
        ///     1. Tool Installation scenarios #1, 2, and 8 as well as
        ///     2. Tool Removal scenarios 1 and 5. 
        ///     3. Because this test uses the LicenseFormHandler, it
        /// also verifies the test plan's License Terms Dialog scenario #7.
        /// </summary>
        [Test]
        public void InstallOneToolTest()
        {
            tripCount = 0;
            // Delete all MSI files to force downloads the first time
            string[] installPackageFiles = Directory.GetFiles(Application.CommonAppDataPath, "*.msi");
            foreach (string filename in installPackageFiles)
            {
                File.Delete(filename);
            }

            errorMessages = new ErrorMessages();

            // Set up to do the install with download
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallOneToolHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(OneToolInstallProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();

            // Setup to remove MSE
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(RemoveOneToolHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(OneToolRemovalProgressFormHandler);
            packInstallForm.ShowDialog();

            // Set up to do the install without download
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallOneToolHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(OneToolInstallProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();

            // Setup to remove MSE
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(RemoveOneToolHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(OneToolRemovalProgressFormHandler);
            packInstallForm.ShowDialog();

            errorMessages.printErrors("InstallOneToolTest");
            Assert.AreEqual(0, errorMessages.Count, "InstallOneToolTest failed. Check Console output for error messages.");
        }

        public void RemoveOneToolHandler(object sender, EventArgs e)
        {
            ButtonTester exitButtonTester = new ButtonTester("exitButton", "PackInstallerForm");

            if ((tripCount == 1) || (tripCount ==3))
            {
                if (errorMessages.Count < 2)
                {
                    // remove the tool.
                    tripCount++;
                    RemoveOneToolLogic();
                }
                else exitButtonTester.Click();
            }
            else
            {
                // The GUI will refresh after the progress window is closed and we end up here.
                // So, just click the Exit button.
                exitButtonTester.Click();
            }
        }

        public void InstallOneToolHandler(object sender, EventArgs e)
        {
            ButtonTester exitButtonTester = new ButtonTester("exitButton", "PackInstallerForm");
            if (tripCount == 0)
            {
                tripCount++;
                // Download and install the tool 
                InstallOneToolLogic();
            }
            else if (tripCount == 2)
            {
                tripCount++;
                if (errorMessages.Count == 0)
                {
                    // Reinstall the tool without downloading it.
                    string installPackageFileName = Path.Combine(Application.CommonAppDataPath, "MSESetup.msi");
                    if (!File.Exists(installPackageFileName))
                    {
                        errorMessages.Add("MSESetup.msi was not found.");
                    }
                    else
                    {
                        InstallOneToolLogic();
                    }
                }
                else exitButtonTester.Click();
            }
            else
            {
                // The GUI will refresh after the progress window is closed and we end up here.
                // So, just click the Exit button.
                exitButtonTester.Click();
            }
        }

        private void InstallOneToolLogic()
        {
            ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");
            // Verify that the Install button is disabled
            if (installSoftwareButtonTester.Properties.Enabled == true)
            {
                errorMessages.Add("Install software button is enabled before software was selected.");
            }

            // Check the box to install the Managed Stack Explorer
            CheckBoxTester ManagedStackExplorertoolCheckBoxTester = new CheckBoxTester("Tools and Utilities.toolsPanel.Managed Stack Explorer.layoutPanel.installToolCheckBox", "PackInstallerForm");
            ManagedStackExplorertoolCheckBoxTester.Click();

            // Verify that the Install button is now enabled
            if (installSoftwareButtonTester.Properties.Enabled != true)
            {
                errorMessages.Add("Install software button Properties.Enabled != true after one tool was selected.");
            }

            // Need an ExpectModal for the progress form. Also need a delegate from the progress form
            // and a handler to close the forms.
            ExpectModal("ProgressResultsForm", "GenericFormHandler");
            ExpectModal("LicenseAgreementDownloadForm", "GenericFormHandler");
            ExpectModal("MultipleLicenseAgreementForm", "LicenseFormHandler");
            installSoftwareButtonTester.Click();
        }

        private void RemoveOneToolLogic()
        {
            ControlFinder managedStackExplorerInstalledBox = new ControlFinder("Tools and Utilities.toolsPanel.Managed Stack Explorer.layoutPanel.glyphLayout.installedBox", packInstallForm);
            RadioButtonTester radioTester = new RadioButtonTester("headerPanel.installationRadio", "PackInstallerForm");
            bool explorerInstalled = false;

            // Make sure we are in install mode
            radioTester.Click();

            // If the Managed Stack Explorer was installed, the Pack Installer makes the 
            // installedBox control visible on the MSE's layout panel. So, if we search for the
            // control and do not find it, the Find method throws a NoSuchControlException which 
            // indicates that installation failed or there is a bug in the Pack Installer.
            try
            {
                managedStackExplorerInstalledBox.Find();
                explorerInstalled = true;
            }
            catch (NoSuchControlException e)
            {
                errorMessages.Add("Managed Stack Explorer is not installed. Caught " + e.ToString());
            }

            if (explorerInstalled == true)
            {
                CheckBoxTester managedStackExplorertoolCheckBoxTester = new CheckBoxTester("Tools and Utilities.toolsPanel.Managed Stack Explorer.layoutPanel.installToolCheckBox", "PackInstallerForm");
                if (managedStackExplorertoolCheckBoxTester.Properties.Enabled == true)
                {
                    errorMessages.Add("Managed Stack Explorer is installed but its checkbox is enabled in Install mode");
                }
                // Verify that all other tools have an enabled checkbox
                PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");
                foreach (Control pack in packListControlTester.InnerControls)
                {
                    PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                    foreach (Control tool in toolsPanelTester.InnerControls)
                    {
                        CheckBoxTester toolCheckBoxTester = new CheckBoxTester(pack.Name + ".toolsPanel." + tool.Name + ".layoutPanel.installToolCheckBox", "PackInstallerForm");
                        if ((tool.Name != "Managed Stack Explorer") && (!tool.Name.Contains ("."))
                            && (toolCheckBoxTester.Properties.Enabled == false))
                        {
                            errorMessages.Add("Tool '" + tool.Name + "' in Pack '" + pack.Name + "' was not installed but is not enabled in installation mode.");
                        }
                    }
                }
                // Click the maintenanceRadio button
                radioTester = new RadioButtonTester("headerPanel.maintenanceRadio", "PackInstallerForm");
                radioTester.Click();
                if (managedStackExplorertoolCheckBoxTester.Properties.Enabled == false)
                {
                    errorMessages.Add("Managed Stack Explorer is installed but its checkbox is not enabled in maintenance mode");
                }
                else
                {
                    // Verify that all other tool checkboxes are disabled
                    foreach (Control pack in packListControlTester.InnerControls)
                    {
                        PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                        foreach (Control tool in toolsPanelTester.InnerControls)
                        {
                            CheckBoxTester toolCheckBoxTester = new CheckBoxTester(pack.Name + ".toolsPanel." + tool.Name + ".layoutPanel.installToolCheckBox", "PackInstallerForm");
                            if ((tool.Name != "Managed Stack Explorer") && (!tool.Name.Contains("."))
                                && (toolCheckBoxTester.Properties.Enabled == true))
                            {
                                errorMessages.Add("Tool '" + tool.Name + "' in Pack '" + pack.Name + "' was not installed but is enabled in maintenance mode.");
                            }
                        }
                    }
                    managedStackExplorertoolCheckBoxTester.Click();
                    ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
                    installSoftwareButtonTester.Click();
                }
            }
        }

        /// <summary>
        /// This form handler clicks the I Accept button on the licensse agreement form
        /// It verifies the test plan's License Terms Dialog scenario #7.
        /// This form handler is used by the InstallOneToolTest and the InstallAllPacksTest
        /// </summary>
        public void LicenseFormHandler()
        {
            ButtonTester buttonTester = new ButtonTester("acceptButton", "MultipleLicenseAgreementForm");
            if (buttonTester.Text != "I &Accept")
            {
                errorMessages.Add("License Form accept button text is wrong '" + buttonTester.Text + "'");
            }
            buttonTester.Click();
        }

        public void OneToolInstallProgressFormHandler(object sender, EventArgs e)
        {
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            bool foundDownLoadingLine = false;
			string successMessage = "1 / 1 tools installed successfully.";
			string downloadingMessage = "Download of Managed Stack Explorer is starting...";

            // Verify that the promoPictureBox control is shown
            PictureBoxTester pictureBoxTester = new PictureBoxTester("promoPictureBox", "ProgressResultsForm");
            try
            {
                // Since NUnitForms doesn't actually find the control until you use it, we
                // check an arbitrary property to verify that the control is there
                // If NUnitForms can't find the control, it throws NoSuchControlException
                // Which we catch and handle
                if (pictureBoxTester.Properties.BackColor != System.Drawing.Color.FromArgb(((int)(((byte)(228)))), ((int)(((byte)(235)))), ((int)(((byte)(248))))))
                {
                    errorMessages.Add("BackColor on promoPictureBox is wrong.");
                }
            }
            catch (NoSuchControlException)
            {
                errorMessages.Add("promoPictureBox was not found.");
            }

            string[] linesInTextBox = textBoxTester.Properties.Lines;
            foreach (string s in linesInTextBox)
            {
                if (s.Contains(downloadingMessage))
                    foundDownLoadingLine = true;
            }
            if (tripCount == 1)
            {
                if (foundDownLoadingLine == false)
                {
                    errorMessages.Add("The expected download of Managed Stack Explorer does not seem to have happened.");
                }
            }
            else
            {
                if (foundDownLoadingLine == true)
                {
                    errorMessages.Add("Regression of Bug 145475: An unexpected download of Managed Stack Explorer seems to have happened.");
                }
            }

            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)
            {
                errorMessages.Add("Tool installation seems to have failed.");
            }
            // Close the progress form
            buttonTester.Click();
        }

        public void OneToolRemovalProgressFormHandler(object sender, EventArgs e)
        {
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            string successMessage = "1 / 1 tools removed successfully.";

            // Verify that the promoPictureBox control is shown
            PictureBoxTester pictureBoxTester = new PictureBoxTester("promoPictureBox", "ProgressResultsForm");
            try
            {
                // Since NUnitForms doesn't actually find the control until you use it, we
                // check an arbitrary property to verify that the control is there
                // If NUnitForms can't find the control, it throws NoSuchControlException
                // Which we catch and handle
                if (pictureBoxTester.Properties.BackColor != System.Drawing.Color.FromArgb(((int)(((byte)(228)))), ((int)(((byte)(235)))), ((int)(((byte)(248))))))
                {
                    errorMessages.Add("BackColor on promoPictureBox is wrong.");
                }
            }
            catch (NoSuchControlException)
            {
                errorMessages.Add("promoPictureBox was not found.");
            }

            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)
            {
                errorMessages.Add("Tool removal seems to have failed.");
            }
            // Close the progress form
            buttonTester.Click();
        }
        #endregion

        #region Install All Packs Test
        /// <summary>
        /// install  and Remove All packs. This test covers the following scenarios from 
        /// the test plan:
        ///     1. Tool Installation scenarios 3, 4, and 5  
        ///     2. Tool Removal scenario 2. 
        ///     3. Because this test uses the LicenseFormHandler, it
        /// also verifies the test plan's License Terms Dialog scenario #7.
        /// </summary>
        [Test]
        public void InstallAllPacksTest()
        {
            tripCount = 0;
            errorMessages = new ErrorMessages();

            // Tool Installation scenario 3
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(InstallAllProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();

            // Add blank line in error messages to make it easier to find section breaks
            if (errorMessages.Count != 0)
                errorMessages.Add("in Tool Installation scenario 3\n");

            // Tool Removal scenario 2
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(RemoveAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(RemoveAllProgressFormHandler);
            packInstallForm.ShowDialog();

            if (errorMessages.Count != 0)
                errorMessages.Add("in Tool Removal scenario 2\n");

            // Tool Installation scenario 4
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(InstallAllProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(RemoveAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(RemoveAllProgressFormHandler);
            packInstallForm.ShowDialog();

            if (errorMessages.Count != 0)
                errorMessages.Add("in Tool Installation scenario 4\n");

            // Tool Installation scenario 5
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(InstallAllProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();
            packInstallForm = new PackInstallerForm(TestStrings.GeneralTestPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(RemoveAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(RemoveAllProgressFormHandler);
            packInstallForm.ShowDialog();


            errorMessages.printErrors("InstallAllPacksTest");
            Assert.AreEqual(0, errorMessages.Count, "InstallAllPacksTest failed. Check Console output for error messages.");
        }

        public void LicenseAgreementDownloadHandler()
        {
            FormFinder finder = new FormFinder();
            NUnit.Extensions.Forms.FormCollection found = finder.FindAll("LicenseAgreementDownloadForm");
            foreach (Form f in found)
            {
                Control.ControlCollection controls = f.Controls;
                foreach (Control c in controls)
                {
                    if (c.Name == "closeButton")
                    {
                        Button b = (Button)c;
                        b.PerformClick();
                        break;
                    }
                }
            }
        }

        private int installAllToolCount = 0;

        /// <summary>
        /// Clicks all packs and the install button for the InstallAllPacksTest
        /// </summary>
        public void InstallAllToolsLogic()
        {
            RadioButtonTester radioTester = new RadioButtonTester("headerPanel.installationRadio", "PackInstallerForm");
            ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");

            // Make sure we are in install mode
            radioTester.Click();
       
            // Install all tools
            installAllToolCount = 0;
            foreach (Control pack in packListControlTester.InnerControls)
            {
                CheckBoxTester installPackCheckBoxTester = new CheckBoxTester(pack.Name + ".headerPanel.installPackCheckBox");
                // Check each Pack for installation
                installPackCheckBoxTester.Click();
                // Count the tools
                PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                foreach (Control tool in toolsPanelTester.InnerControls)
                {
                    CheckBoxTester toolCheckBoxTester = new CheckBoxTester(pack.Name + ".toolsPanel." + tool.Name + ".layoutPanel.installToolCheckBox", "PackInstallerForm");
                    
                    if ((!tool.Name.Contains (".") && toolCheckBoxTester.Properties.CheckState != CheckState.Checked))
                    {
                        ToolSummaryControlTester toolSummaryTester = new ToolSummaryControlTester(pack.Name + ".toolsPanel." + tool.Name, "PackInstallerForm");
                        string installedText = " but does not seem to be installed.";
                        foreach (Control innerControl in toolSummaryTester.InnerControls)
                        {
                            if (innerControl.Name == "installedBox")
                            {
                                installedText = " because it is installed.";
                                break;
                            }
                        }
                        errorMessages.Add("Tool '" + tool.Name + "' in Pack '" + pack.Name + "' is not checked" + installedText);
                    }
                    installAllToolCount++;
                }
            }

            // Verify that the Install button is now enabled
            if (installSoftwareButtonTester.Properties.Enabled != true)
            {
                errorMessages.Add("InstallAllToolsLogic: Install software button is disabled after packs are selected.");
            }
            ExpectModal("ProgressResultsForm", "GenericFormHandler");
            ExpectModal("LicenseAgreementDownloadForm", "LicenseAgreementDownloadHandler");
            ExpectModal("MultipleLicenseAgreementForm", "LicenseFormHandler");
            installSoftwareButtonTester.Click();
        }

        /// <summary>
        /// This progress form handler verifies the test plan's 
        /// Progress Results Dialog scenario #1. Specifically, it verifies that
        /// the progress form's text box contains messages that inform the user that
        /// a download has started and whether or not it successfuly completed as well as the 
        /// start and completion of an installation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void InstallAllProgressFormHandler(object sender, EventArgs e)
        {
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            bool foundInstallingLine = false;
            bool foundDownLoadedLine = false;
            bool foundDownloadingLine = false;
            bool differentMSI = false;
            string successMessage = installAllToolCount + " / " + installAllToolCount + " tools installed successfully.";
            string downloadingPrefix = "Download of ";
            string installingPrefix = "Installation of ";
            string startingSuffix = " is starting...";
			string downloadedMessage = " downloaded successfully.";
			string installedMessage = " installed successfully.";
            string differentMSIDownloaded = "The file downloaded for ";
            string diffMSISuffix = " did not match the file that was expected.";

            // Verify that the promoPictureBox control is shown
            PictureBoxTester pictureBoxTester = new PictureBoxTester("promoPictureBox", "ProgressResultsForm");
            try
            {
                // Since NUnitForms doesn't actually find the control until you use it, we
                // check an arbitrary property to verify that the control is there
                // If NUnitForms can't find the control, it throws NoSuchControlException
                // Which we catch and handle
                if (pictureBoxTester.Properties.BackColor != System.Drawing.Color.FromArgb(((int)(((byte)(228)))), ((int)(((byte)(235)))), ((int)(((byte)(248))))))
                {
                    errorMessages.Add("BackColor on promoPictureBox is wrong.");
                }
            }
            catch (NoSuchControlException)
            {
                errorMessages.Add("promoPictureBox was not found.");
            }

            string[] linesInTextBox = textBoxTester.Properties.Lines;

            foreach (Control pack in packListControlTester.InnerControls)
            {
                PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                foreach (Control tool in toolsPanelTester.InnerControls)
                {
                    #region Determine whether or not each tool was downloaded
                    foundDownLoadedLine = false;
                    foundDownloadingLine = false;
                    differentMSI = false;
                    string downloadingMessage = downloadingPrefix + tool.Name + startingSuffix;
                    string successfullyDownloadedMessage = tool.Name + downloadedMessage;
                    string differntMSIMessage = differentMSIDownloaded + tool.Name + diffMSISuffix;
                    foreach (string s in linesInTextBox)
                    {
                        if (s.Contains(downloadingMessage))
                            foundDownloadingLine = true;
                        if (s.Contains(successfullyDownloadedMessage))
                        {
                            foundDownLoadedLine = true;
                        }
                        if (s.Contains(differntMSIMessage))
                        {
                            differentMSI = true;
                            break;
                        }
                    }
                    if (tripCount == 1)
                    {
                        if (foundDownloadingLine == false)
                        {
                            errorMessages.Add("InstallAllProgressFormHandler: Expected download of " + tool.Name + " does not seem to have occurred in trip " + tripCount);
                        }
                        if (foundDownLoadedLine == false)
                        {
                            errorMessages.Add("InstallAllProgressFormHandler: Download of " + tool.Name + " failed in trip " + tripCount);
                        }
                        if (differentMSI == true)
                        {
                            errorMessages.Add("InstallAllProgressFormHandler: Different MSI downloaded for " + tool.Name + " the " + TestStrings.GeneralTestPackFeedXml + " file may be out of date.");
                        }
                    }
                    else if (tripCount == 3)
                    {
                        if (foundDownLoadedLine == true)
                        {
                            errorMessages.Add("InstallAllProgressFormHandler: Unexpected download of " + tool.Name + " occurred in trip " + tripCount);
                        }
                    }
                    else if (tripCount == 5)
                    {
                        if ((tool.Name == "Managed Stack Explorer") || (tool.Name == "VSCommand Shell"))
                        {
                            if (foundDownLoadedLine == false)
                            {
                                errorMessages.Add("InstallAllProgressFormHandler: Expected download of " + tool.Name + " does not seem to have occurred in trip " + tripCount);
                            }
                        }
                        else
                        {
                            if (foundDownLoadedLine == true)
                            {
                                errorMessages.Add("InstallAllProgressFormHandler: Unexpected download of " + tool.Name + " occurred in trip " + tripCount);
                            }
                        }
                    }
                    #endregion

                    #region Check for installation success.
                    bool foundSuccessLine = false;
                    foundInstallingLine = false;
                    string installingMessage = installingPrefix + tool.Name + startingSuffix;
                    string successfullyInstalledMessage = tool.Name + installedMessage;
                    foreach (string s in linesInTextBox)
                    {
                        if (s.Contains(installingMessage))
                            foundInstallingLine = true;
                        if (s.Contains(successfullyInstalledMessage))
                        {
                            foundSuccessLine = true;
                            break;
                        }
                    }
                    if (foundInstallingLine == false)
                    {
                        errorMessages.Add("InstallAllProgressFormHandler: Starting to install " + tool.Name + " message not found in trip " + tripCount);
                    }

                    if (foundSuccessLine == false)
                    {
                        errorMessages.Add("InstallAllProgressFormHandler: Installation of " + tool.Name + " failed in trip " + tripCount);
                    }
                    #endregion
                }
            }

            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)
            {
                errorMessages.Add("InstallAllProgressFormHandler: Tool installation seems to have failed in trip " + tripCount + 
                    "\n\tExpected message was '" + successMessage +
                    "'\n\t queueCompleteLabel text =='" + ct.Text + "'");
            }

            // Close the progress form
            buttonTester.Click();
        }

         /// <summary>
        ///  This is the delegate callback method that contains the Remove All test logic.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>       
        public void RemoveAllHandler(object sender, EventArgs e)
        {
            ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");

            if ((tripCount == 1) || (tripCount == 3) || (tripCount == 5))
            {
                //This section removes all tools
                tripCount++;

                // Click the maintenanceRadio button
                RadioButtonTester radioTester = new RadioButtonTester("headerPanel.maintenanceRadio", "PackInstallerForm");
                radioTester.Click();

                // Remove All packs
                foreach (Control pack in packListControlTester.InnerControls)
                {
                    PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                    CheckBoxTester installPackCheckBoxTester = new CheckBoxTester(pack.Name + ".headerPanel.installPackCheckBox");
                    // Check each Pack for installation
                    installPackCheckBoxTester.Click();
                    // Verify that each tool in the pack is checked
                    foreach (Control tool in toolsPanelTester.InnerControls)
                    {
                        CheckBoxTester toolCheckBoxTester = new CheckBoxTester(pack.Name + ".toolsPanel." + tool.Name + ".layoutPanel.installToolCheckBox", "PackInstallerForm");
                        if ((!tool.Name.Contains (".") && toolCheckBoxTester.Properties.CheckState != CheckState.Checked))
                        {
                            errorMessages.Add("Tool '" + tool.Name + "' in Pack '" + pack.Name + "' is not checked after clicking pack checkbox in maintenance mode.");
                        }
                    }
                }
                installSoftwareButtonTester.Click();
            }
            else
            {
                packInstallForm.Close();
            }
        }


        /// <summary>
        ///  This is the delegate callback method that contains the Install All test logic.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void InstallAllHandler(object sender, EventArgs e)
        {
            ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");

            if (tripCount == 0)
            {
                tripCount++;
                // This section will download and install all tools.
                // Delete all MSI files to force downloads
                string[] installPackageFiles = Directory.GetFiles(Application.CommonAppDataPath, "*.msi");
                foreach (string filename in installPackageFiles)
                {
                    File.Delete(filename);
                }
                // Install the tools
                InstallAllToolsLogic();
            }
            else if (tripCount == 2)
            {
                // This section will install all tools without downloads
                tripCount++;
                InstallAllToolsLogic();
            }
            else if (tripCount == 4)
            {
                // This section will install all tools after deleting two 
                // msi files so that two tools must be downloaded and 
                // others are previously downloaded.
                tripCount++;
                string installPackageFileName = Path.Combine(Application.CommonAppDataPath, "MSESetup.msi");
                File.Delete(installPackageFileName);
                installPackageFileName = Path.Combine(Application.CommonAppDataPath, "VSCmdShellSetup.msi");
                File.Delete(installPackageFileName);
                // Do the installation
                InstallAllToolsLogic();
            }
            else
            {
                packInstallForm.Close();
            }
        }

        /// <summary>
        /// The delegate callback method which contains verification logic for
        /// the removal portion of the InstallAllPacksTest
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RemoveAllProgressFormHandler(object sender, EventArgs e)
        {
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            bool foundSuccessLine = false;
            bool foundStartingLine = false;
            string successMessage = installAllToolCount + " / " + installAllToolCount + " tools removed successfully.";
			string installedMessage = " was removed successfully.";
            string unistallStartPrefix = "Removal of ";
            string startingSuffix = " is starting...";

            // Verify that the promoPictureBox control is shown
            PictureBoxTester pictureBoxTester = new PictureBoxTester("promoPictureBox", "ProgressResultsForm");
            try
            {
                // Since NUnitForms doesn't actually find the control until you use it, we
                // check an arbitrary property to verify that the control is there
                // If NUnitForms can't find the control, it throws NoSuchControlException
                // Which we catch and handle
                if (pictureBoxTester.Properties.BackColor != System.Drawing.Color.FromArgb(((int)(((byte)(228)))), ((int)(((byte)(235)))), ((int)(((byte)(248))))))
                {
                    errorMessages.Add("BackColor on promoPictureBox is wrong.");
                }
            }
            catch (NoSuchControlException)
            {
                errorMessages.Add("promoPictureBox was not found.");
            }

            string[] linesInTextBox = textBoxTester.Properties.Lines;

            foreach (Control pack in packListControlTester.InnerControls)
            {
                PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                foreach (Control tool in toolsPanelTester.InnerControls)
                {
                    #region Check for removal success.
                    foundSuccessLine = false;
                    foundStartingLine = false;
                    string successfullyInstalledMessage = tool.Name + installedMessage;
                    string startingMessage = unistallStartPrefix + tool.Name + startingSuffix;
                    foreach (string s in linesInTextBox)
                    {
                        if (s.Contains(startingMessage))
                            foundStartingLine = true;
                        if (s.Contains(successfullyInstalledMessage))
                        {
                            foundSuccessLine = true;
                            break;
                        }
                    }
                    if (foundStartingLine == false)
                    {
                        errorMessages.Add("RemoveAllProgressFormHandler: Starting removal of " + tool.Name + " message was not found.");
                    }
                    if (foundSuccessLine == false)
                    {
                        errorMessages.Add("RemoveAllProgressFormHandler: Removal of " + tool.Name + " failed.");
                    }
                    #endregion
                }

            }
            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)

            {
                errorMessages.Add("RemoveAllProgressFormHandler: Remove all seems to have failed." + 
                    "Expected message was '" + successMessage + 
                    "'\n\t queueCompleteLabel text =='" + ct.Text + "'");
            }

            // Close the progress form
            buttonTester.Click();
        }

        #endregion

        #region Exclude tools with different MSI Tests
        string[] excludedToolNames = { "101 Samples for Visual Studio 2005 (Visual C#)", "101 Samples for Visual Studio 2005 (Visual Basic)" };
        /// <summary>
        /// 
        /// </summary>
        [Test]
        public void ExcludeDifferentMSITest()
        {
            tripCount = 0;
            errorMessages = new ErrorMessages();

            // Attempt to install all tools in the feed file. Two should fail because
            // the hash code in the feed file does not match the downloaded file
            packInstallForm = new PackInstallerForm(TestStrings.BadCRCPackFeed);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallAllHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(InstallExcludeProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();

            // Remove All tools that were installed
            packInstallForm = new PackInstallerForm(TestStrings.BadCRCPackFeed);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(RemoveExcludeHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(RemoveExcludeProgressFormHandler);
            packInstallForm.ShowDialog();

            errorMessages.printErrors("ExcludeDifferentMSITest");
            Assert.AreEqual(0, errorMessages.Count, "ExcludeDifferentMSITest failed. Check Console output for error messages.");
        }

        /// <summary>
        /// Verification logic for the install portion of the ExcludeDifferentMSI Test.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void InstallExcludeProgressFormHandler(object sender, EventArgs e)
        {
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            bool foundSuccessLine = false;
            bool foundInstallingLine = false;
            bool foundDownLoadedLine = false;
            bool foundDownloadingLine = false;
            bool differentMSI = false;
            string successMessage = (installAllToolCount -2) + " / " + installAllToolCount + " tools installed successfully.";
            string downloadingPrefix = "Download of ";
            string installingPrefix = "Installation of ";
            string startingSuffix = " is starting...";
            string downloadedMessage = " downloaded successfully.";
            string installedMessage = " installed successfully.";
            string differentMSIDownloaded = "The file downloaded for ";
            string diffMSISuffix = " did not match the file that was expected.";

            string[] linesInTextBox = textBoxTester.Properties.Lines;

            foreach (Control pack in packListControlTester.InnerControls)
            {
                PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                foreach (Control tool in toolsPanelTester.InnerControls)
                {
                    #region Determine whether or not each tool was downloaded
                    foundDownLoadedLine = false;
                    foundDownloadingLine = false;
                    differentMSI = false;
                    string downloadingMessage = downloadingPrefix + tool.Name + startingSuffix;
                    string successfullyDownloadedMessage = tool.Name + downloadedMessage;
                    string differntMSIMessage = differentMSIDownloaded + tool.Name + diffMSISuffix;
                    foreach (string s in linesInTextBox)
                    {
                        if (s.Contains(downloadingMessage))
                            foundDownloadingLine = true;
                        if (s.Contains(successfullyDownloadedMessage))
                        {
                            foundDownLoadedLine = true;
                        }
                        if (s.Contains(differntMSIMessage))
                        {
                            differentMSI = true;
                            break;
                        }
                    }
                    // All msi files should be downloaded
                    if (foundDownloadingLine == false)
                    {
                        errorMessages.Add("InstallExcludeProgressFormHandler: Expected download of " + tool.Name + " does not seem to have occurred." );
                    }
                    // All msi files should download successfully
                    if (foundDownLoadedLine == false)
                    {
                        errorMessages.Add("InstallExcludeProgressFormHandler: Download of " + tool.Name + " failed. ");
                    }

                    if (differentMSI == true)
                    {
                        // Flag an error if a tool other than the two expected tools download the "wrong" MSI 
                        if ((tool.Name != excludedToolNames[0]) && (tool.Name != excludedToolNames[1]))
                        {
                            errorMessages.Add("InstallExcludeProgressFormHandler: Different MSI downloaded for " + tool.Name + " the " + TestStrings.BadCRCPackFeed + " file may be out of date.");
                        }
                    }
                    // if either of the the two expected tools do NOT download the "wrong" MSI, it's an error
                    else if ((tool.Name == excludedToolNames[0]) || (tool.Name == excludedToolNames[1]))
                    {
                        errorMessages.Add("InstallExcludeProgressFormHandler: expected the " + tool.Name + " tool to download an MSI with a different CRC. It Didn't.");
                    }

                    #endregion

                    #region Check for installation success.
                    foundSuccessLine = false;
                    foundInstallingLine = false;
                    string installingMessage = installingPrefix + tool.Name + startingSuffix;
                    string successfullyInstalledMessage = tool.Name + installedMessage;
                    foreach (string s in linesInTextBox)
                    {
                        if (s.Contains(installingMessage))
                            foundInstallingLine = true;
                        if (s.Contains(successfullyInstalledMessage))
                        {
                            foundSuccessLine = true;
                            break;
                        }
                    }
                    if (foundInstallingLine == false)
                    {
                        if ((tool.Name != excludedToolNames[0]) && (tool.Name != excludedToolNames[1]))
                        {
                            errorMessages.Add("InstallExcludeProgressFormHandler: Starting to install " + tool.Name + " message not found ");
                        }
                    }
                    else if ((tool.Name == excludedToolNames[0]) || (tool.Name == excludedToolNames[1]))
                    {
                        errorMessages.Add("InstallExcludeProgressFormHandler: Unexpectedly found starting to install message for " + tool.Name);
                    }

                    if (foundSuccessLine == false)
                    {
                        if ((tool.Name != excludedToolNames[0]) && (tool.Name != excludedToolNames[1]))
                        {

                            errorMessages.Add("InstallExcludeProgressFormHandler: Installation of " + tool.Name + " failed ");
                        }
                    }
                    else if ((tool.Name == excludedToolNames[0]) || (tool.Name == excludedToolNames[1]))
                    {
                        errorMessages.Add("InstallExcludeProgressFormHandler: Unexpectedly found successful installation of " + tool.Name);
                    }
                    #endregion
                }
            }

            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)

            {
                errorMessages.Add("InstallExcludeProgressFormHandler: Tool installation step seems to have failed " + 
                    "\n\tExpected string was '" + successMessage +
                    "'\n\tqueueCompleteLabel text =='" + ct.Text + 
                    "'");
            }

            // Close the progress form
            buttonTester.Click();
        }

        /// <summary>
        /// Tool removal logic for the ExcludeDifferentMSI Test.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>       
        public void RemoveExcludeHandler(object sender, EventArgs e)
        {
            ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");

            if ((tripCount == 1) )
            {
                //This section removes all tools that were installed
                tripCount++;

                // Click the maintenanceRadio button
                RadioButtonTester radioTester = new RadioButtonTester("headerPanel.maintenanceRadio", "PackInstallerForm");
                radioTester.Click();

                // Remove All packs
                foreach (Control pack in packListControlTester.InnerControls)
                {
                    PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                    CheckBoxTester installPackCheckBoxTester = new CheckBoxTester(pack.Name + ".headerPanel.installPackCheckBox");
                    // Check each visible Pack for removal
                    if (installPackCheckBoxTester.Properties.Visible == true)
                    {
                        installPackCheckBoxTester.Click();
                    }
                }
                installSoftwareButtonTester.Click();
            }
            else
            {
                packInstallForm.Close();
            }
        }


        /// <summary>
        /// Verification logic for the tool removal portion of the ExcludeDifferentMSI Test.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RemoveExcludeProgressFormHandler(object sender, EventArgs e)
        {
            PanelControlTester packListControlTester = new PanelControlTester("packListPanel", "PackInstallerForm");
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            bool foundSuccessLine = false;
            bool foundStartingLine = false;
            int installedToolCount = installAllToolCount-2;
            string successMessage = "2 / 2 tools removed successfully.";
            string installedMessage = " was removed successfully.";
            string unistallStartPrefix = "Removal of ";
            string startingSuffix = " is starting...";

            string[] linesInTextBox = textBoxTester.Properties.Lines;

            foreach (Control pack in packListControlTester.InnerControls)
            {
                PanelControlTester toolsPanelTester = new PanelControlTester(pack.Name + ".toolsPanel", "PackInstallerForm");
                foreach (Control tool in toolsPanelTester.InnerControls)
                {
                    #region Check for removal success.
                    foundSuccessLine = false;
                    foundStartingLine = false;
                    string successfullyInstalledMessage = tool.Name + installedMessage;
                    string startingMessage = unistallStartPrefix + tool.Name + startingSuffix;
                    foreach (string s in linesInTextBox)
                    {
                        if (s.Contains(startingMessage))
                            foundStartingLine = true;
                        if (s.Contains(successfullyInstalledMessage))
                        {
                            foundSuccessLine = true;
                            break;
                        }
                    }
                    if ((foundStartingLine == false) &&
                        ((tool.Name != excludedToolNames[0]) && (tool.Name != excludedToolNames[1])))
                    {
                        errorMessages.Add("RemoveExcludeProgressFormHandler: Starting removal of " + tool.Name + " message was not found.");
                    }
                    if ((foundSuccessLine == false) &&
                        ((tool.Name != excludedToolNames[0]) && (tool.Name != excludedToolNames[1])))
                    {
                        errorMessages.Add("RemoveExcludeProgressFormHandler: Removal of " + tool.Name + " failed.");
                    }
                    #endregion
                }

            }
            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)
            {
                errorMessages.Add("RemoveExcludeProgressFormHandler: Remove all seems to have failed." + "\n\tExpected string was '" + successMessage +
                    "'\n\tqueueCompleteLabel text == '" + ct.Text + "'");
            }

            // Close the progress form
            buttonTester.Click();
        }

        #endregion


        #region Install a VSI file

        [Test]
        public void InstallVSIToolTest()
        {
            tripCount = 0;
            errorMessages = new ErrorMessages();

            // Install VSI content from .vsi files and a .zip file
            packInstallForm = new PackInstallerForm(TestStrings.InstallVSIContentPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallVSIToolHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(VsiToolInstallProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");
            packInstallForm.ShowDialog();

            // Installing a second time should work - 
            packInstallForm = new PackInstallerForm(TestStrings.InstallVSIContentPackFeedXml);
            packInstallForm.OnPacksLoaded += new EventHandler<EventArgs>(InstallVSIToolHandler);
            packInstallForm.OnToolsInstalled += new EventHandler<EventArgs>(VsiToolInstallProgressFormHandler);
            ExpectModal("PackInstallerForm", "GenericFormHandler");

            packInstallForm.ShowDialog();

            errorMessages.printErrors("InstallVSIToolTest");
            Assert.AreEqual(0, errorMessages.Count, "InstallVSIToolTest failed. Check Console output for error messages.");
        }

        public void VsiToolInstallProgressFormHandler(object sender, EventArgs e)
        {
            RichTextBoxTester textBoxTester = new RichTextBoxTester("progressResultsTextBox", "ProgressResultsForm");
            ButtonTester buttonTester = new ButtonTester("cancelCloseButton", "ProgressResultsForm");
            string successMessage = "3 / 3 tools installed successfully.";

            ControlTester ct = new ControlTester("queueCompleteLabel", "ProgressResultsForm");
            if (ct.Text != successMessage)
            {
                errorMessages.Add("VSI Tool installation seems to have failed.");
            }

            // Close the progress form
            FormTester formTester = new FormTester("ProgressResultsForm");
            formTester.Close();
        }

        public void InstallVSIToolHandler(object sender, EventArgs e)
        {
            // Delete files installed by this test so that we do not get the 
            // CopyConflictDialog dialog box. 

            string path = Environment.GetEnvironmentVariable("USERPROFILE") +
                                @"\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets\";
            if (Directory.Exists(path))
            {
                foreach (string filename in snippetNames)
                {
                    File.Delete(path + filename);
                }
            }
            path = Environment.GetEnvironmentVariable("USERPROFILE") +
                                @"\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual C#\Starter Kits\Teacher Starter Kit (C#).zip";
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            ButtonTester exitButtonTester = new ButtonTester("exitButton", "PackInstallerForm");

            if (tripCount == 0)
            {
                tripCount++;
                // Download and install the VSI tool 
                InstallVSIToolLogic();
                packInstallForm.Close();
            }
            else if (tripCount == 1)
            {
                if (errorMessages.Count == 0)
                {
                    tripCount++;
                    // Install it again but this time we should get the CopyConflictDialog
                    InstallVSIToolLogic();
                }
                packInstallForm.Close();
            }
            else
            {
                packInstallForm.Close();
            }
        }

        #region Names of snippet files installed by InstallVSIToolTest
        static string[] snippetNames = 
                {
                    "ActivateaRunningApplicationByName.snippet",
                    "ActivateaRunningApplicationByProcess.snippet",
                    "CalculateaMonthlyPaymentonaLoan.snippet",
                    "CalculateCosineOfAnAngle.snippet",
                    "CalculateDepreciation.snippet",
                    "CalculateSineOfAnAngle.snippet",
                    "CalculateTangentOfAnAngle.snippet",
                    "CaptureOutputfromaConsoleApplication.snippet",
                    "ChangetheForegroundandBackgroundTextColorsinaConsoleWindow.snippet",
                    "CheckforCurrentVersionusingClickOnce.snippet",
                    "CheckForUpdates.snippet",
                    "CleartheConsoleWindow.snippet",
                    "DeleteDatafromIsolatedStorage.snippet",
                    "DeterminetheAmountofIsolatedStorageAvailable.snippet",
                    "DeterminetheScopeofIsolatedStorage.snippet",
                    "DownloadFilesSynchronouslyClickOnce.snippet",
                    "FindtheNamesofResourcesinanAssembly.snippet",
                    "GetARandomNumber.snippet",
                    "LoadaCursorFilefromanAssembly.snippet",
                    "LoadanAssemblyatRunTime.snippet",
                    "MakeanAsynchronousMethodCall.snippet",
                    "ReadandWritefromtheConsoleWindow.snippet",
                    "ReadCommandLineArguments.snippet",
                    "ReadTextfromIsolateStorage.snippet",
                    "RetrieveanImagethatIsanApplicationResource.snippet",
                    "RetrieveaTextFilefromanAssembly'sResources.snippet",
                    "Savetextinisolatedstorage.snippet",
                    "SendAnApplicationKeystrokes.snippet",
                    "StopAnApplication.snippet",
                    "UpdateApplicationusingClickOnce.snippet",
                    "UpdateClickonceApplicationSynchronously.snippet",
                    "WriteaMessagetotheApplicationEventLog.snippet",
                };
        #endregion

        private void InstallVSIToolLogic()
        {
            ButtonTester installSoftwareButtonTester = new ButtonTester("installSoftwareButton", "PackInstallerForm");
            CheckBoxTester vsiToolCheckBoxTester = new CheckBoxTester("Visual Studio Contents.headerPanel.installPackCheckBox", "PackInstallerForm");

            // Check the box to install the VSI tool pack
            vsiToolCheckBoxTester.Click();

            // Verify that the Install button is now enabled
            if (installSoftwareButtonTester.Properties.Enabled != true)
            {
                errorMessages.Add("Install software button is disabled after the VSI tool was selected.");
                ButtonTester exitButtonTester = new ButtonTester("exitButton", "PackInstallerForm");
                exitButtonTester.Click();
            }
            else
            {
                // Need an ExpectModal for the progress form and license agreement download form with a no-op handler
                // Also need an ExpectModal for the license agreement form and a handler to close the form.
                ExpectModal("ProgressResultsForm", "GenericFormHandler");
                ExpectModal("LicenseAgreementDownloadForm", "GenericFormHandler");
                ExpectModal("MultipleLicenseAgreementForm", "LicenseFormHandler");

                installSoftwareButtonTester.Click();
            }
        }

        #endregion

        #region CopyConflictDialogFormTests
        private CopyConflictDialog conflictDialog;
        [Test]
        public void CopyConflictDialogFormTest()
        {
            string path = Environment.GetEnvironmentVariable("USERPROFILE") +
                                @"\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual C#\Starter Kits\Teacher Starter Kit (C#).zip";
            errorMessages = new ErrorMessages();

            conflictDialog = new CopyConflictDialog(path);
            ExpectModal("CopyConflictDialog", "conflictDialogHandler1");
            conflictDialog.ShowDialog();

            // This step should select overwrite and save it
            if (conflictDialog.SelectedOption != CopyConflictDialog.ConflictResolution.Overwrite)
            {
                errorMessages.Add("SelectedOption != Overwrite");
            }
            if (CopyConflictDialog.SavedSetting != CopyConflictDialog.ConflictResolution.Overwrite)
            {
                errorMessages.Add("SavedSetting != Overwrite");
            }

            // This stepshould select new and save it
            conflictDialog = new CopyConflictDialog(path);
            ExpectModal("CopyConflictDialog", "conflictDialogHandler2");
            conflictDialog.ShowDialog();
            if (conflictDialog.SelectedOption != CopyConflictDialog.ConflictResolution.SaveNew)
            {
                errorMessages.Add("SelectedOption != SaveNew");
            }
            if (CopyConflictDialog.SavedSetting != CopyConflictDialog.ConflictResolution.SaveNew)
            {
                errorMessages.Add("SavedSetting != SaveNew");
            }

            // This step selects skip and does not save it. 
            // So, the saved setting should not change
            conflictDialog = new CopyConflictDialog(path);
            ExpectModal("CopyConflictDialog", "conflictDialogHandler3");
            conflictDialog.ShowDialog();
            if (conflictDialog.SelectedOption != CopyConflictDialog.ConflictResolution.Skip)
            {
                errorMessages.Add("SelectedOption != Skip");
            }
            if (CopyConflictDialog.SavedSetting != CopyConflictDialog.ConflictResolution.SaveNew)
            {
                errorMessages.Add("SavedSetting != SaveNew");
            }

            errorMessages.printErrors("CopyConflictDialogUnitTest");
            Assert.AreEqual(0, errorMessages.Count, "CopyConflictDialogUnitTest failed. Check Console output for error messages.");
        }

        public void conflictDialogHandler1()
        {
            CheckBoxTester checkBoxTester = new CheckBoxTester("uxSaveSetting");
            RadioButtonTester radioTester = new RadioButtonTester("uxOverwrite");
            radioTester.Click();
            if (radioTester.Checked == false)
            {
                errorMessages.Add("uxOverwrite is not selected.");
            }
            checkBoxTester.Click();
            if (checkBoxTester.Properties.CheckState != CheckState.Checked)
            {
                errorMessages.Add("uxSaveSetting is not checked.");
            }

            ButtonTester buttonTester = new ButtonTester("uxContinueButton");
            buttonTester.Click();
        }

        public void conflictDialogHandler2()
        {
            CheckBoxTester checkBoxTester = new CheckBoxTester("uxSaveSetting");
            RadioButtonTester radioTester = new RadioButtonTester("uxNew");
            radioTester.Click();
            if (radioTester.Checked == false)
            {
                errorMessages.Add("uxNew is not selected.");
            }
            checkBoxTester.Click();
            if (checkBoxTester.Properties.CheckState != CheckState.Checked)
            {
                errorMessages.Add("uxSaveSetting is not checked.");
            }
            ButtonTester buttonTester = new ButtonTester("uxContinueButton");
            buttonTester.Click();
        }

        public void conflictDialogHandler3()
        {
            CheckBoxTester checkBoxTester = new CheckBoxTester("uxSaveSetting");
            RadioButtonTester radioTester = new RadioButtonTester("uxSkip");
            radioTester.Click();
            if (radioTester.Checked == false)
            {
                errorMessages.Add("uxSkip is not selected.");
            }

            if (checkBoxTester.Properties.CheckState == CheckState.Checked)
            {
                errorMessages.Add("uxSaveSetting is checked.");
            }
            ButtonTester buttonTester = new ButtonTester("uxContinueButton");
            buttonTester.Click();
        }
        #endregion
    }
}
